package com.yunji.base.service;

import com.yunji.base.bo.GetSiteInfoBO;
import com.yunji.base.convert.SiteConvert;
import com.yunji.base.dao.IBindRecordDao;
import com.yunji.base.dao.ISiteDao;
import com.yunji.base.dao.ISiteScaleDao;
import com.yunji.base.dao.IStaffSiteDao;
import com.yunji.base.domain.BindRecordDO;
import com.yunji.base.domain.SiteScaleDO;
import com.yunji.base.domain.StaffSiteDO;
import com.yunji.base.dto.SiteFieldDTO;
import com.yunji.base.vo.SiteInfoVO;
import com.yunji.common.model.domain.SiteDO;
import com.yunji.common.utils.ConvertDataUtil;
import com.yunji.common.utils.bean.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * @version 1.0
 * @Project: yunji-coffee
 * @Package com.yunji.user.service.impl
 * @FileName SiteServiceImpl.java
 * @Copyright Copyright © 2024 云集互联. All Rights Reserved
 * Company		com
 * @CreateTime 2024-05-28 15:01:29
 * @Description 网点接口实现
 * @Modification Author                    History Time			Version				Description
 * --------------------------------------------------------------------------------
 * HuaAo0803@163.com	2024-05-28 15:01:29		1.0					1.0 Version
 * @since JDK 1.8.0_202
 */
@Service
public class SiteService {
    @Resource
    private ISiteDao siteDao;

    @Resource
    private ISiteScaleDao siteScaleDao;

    @Resource
    private IStaffSiteDao staffSiteDao;

    @Resource
    private IBindRecordDao bindRecordDao;

    public List<SiteScaleDO> listScaleBySiteId(Long siteId) {
        return siteScaleDao.listBySiteId(siteId);
    }

    public boolean createBindRecord(BindRecordDO record) {
        return bindRecordDao.save(record);
    }

    public SiteDO getById(Long siteId) {
        return siteDao.getById(siteId);
    }

    public List<SiteScaleDO> listScaleBySiteIds(List<Long> siteIds) {
        return siteScaleDao.listScaleBySiteIds(siteIds);
    }

    /**
     * @param ids 网点ID数组
     * @Title search
     * @Desc 查询网点信息列表
     * @Date 2024-08-19 09:46:12.623
     * @Return List<SiteInfo> 网点信息列表
     */

    public List<SiteDO> search(Long... ids) {
        return siteDao.listByIds(Arrays.asList(ids));
    }

    /**
     * @param site 网点
     * @Title create
     * @Desc 创建网点信息
     * @Date 2024-05-29 09:26:09.002
     * @Return boolean 创建结果
     */
    public boolean create(SiteDO site) {
        return siteDao.save(site);
    }

    /**
     * @param site 网点信息
     * @Title update
     * @Desc 更新网点信息
     * @Date 2024-05-29 09:32:17.707
     * @Return boolean 更新结果
     */

    public boolean update(SiteDO site) {
        return siteDao.updateById(site);
    }

    /**
     * @param ids 网点ID数组
     * @Title delete
     * @Desc 批量删除网点
     * @Date 2024-05-29 09:31:16.264
     * @Return boolean 删除结果
     */

    @Transactional
    public boolean delete(Long... ids) {
        return siteDao.removeByIds(Arrays.asList(ids));
    }

    public boolean checkRepeatName(SiteDO siteName) {
        return siteDao.existSiteName(siteName);
    }

    public SiteDO getSiteByBelongId(Long belongId) {
        return siteDao.getByBelongId(belongId);
    }

    public List<SiteDO> selectSitesByStaffId(Long staffId) {
        List<StaffSiteDO> staffSites = staffSiteDao.listByStaffId(staffId);
        List<Long> siteIds = ConvertDataUtil.list2FieldList(staffSites, StaffSiteDO::getSiteId);
        return siteDao.listByIds(siteIds);
    }

    public List<String> searchTableFiled(SiteFieldDTO param) {
        return siteDao.searchTableFiled(param);
    }


    public GetSiteInfoBO getSiteInfoBO(Long id) {
        return SiteConvert.getSiteAmountBO(id);
    }

    public SiteInfoVO getSiteInfoVO(Long id) {
        SiteDO site = siteDao.getById(id);
        Assert.isTrue(BeanUtils.isNotNull(site), "没有找到网点信息");
        GetSiteInfoBO siteAmountBO = SiteConvert.getSiteInfoBO(site);
        SiteInfoVO info = SiteConvert.getSiteInfoVo(siteAmountBO);
        List<SiteScaleDO> scaleDOList = siteScaleDao.listBySiteId(id);
        info.setSiteScaleList(scaleDOList);
        return info;
    }

}